package demo;

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 王腾 on 2017/3/10.
 */
public class ModelSetMapper implements FieldSetMapper<Model> {
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Model mapFieldSet(FieldSet fieldSet) throws BindException {
        Model model = new Model();
        model.setBusinessName(fieldSet.readString(0));
        model.setMobileFactory(fieldSet.readString(1));
        model.setShenZhenTongNo(fieldSet.readString(2));
        model.setSeid(fieldSet.readString(3));
        model.setOldSeid(fieldSet.readString(4));
        model.setSource(fieldSet.readString(5));
        model.setYizi(fieldSet.readString(6));
        String time = fieldSet.readString(7);
        if (StringUtils.isEmpty(time)) {
            model.setTime(new Date());
        } else {
            try {
                model.setTime(dateFormat.parse(time));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return model;
    }
}
